Imports System
Imports System.Text

Imports Google.GData.AccessControl
Imports Google.GData.Calendar
Imports Google.GData.Client
Imports Google.GData.Extensions

    Class CalendarDemo
        Private Shared userName As String, userPassword As String, feedUri As String

        '<summary>
        'Prints a list of the user's calendars.
        '</summary>
        ' <param name="service">The authenticated CalendarService object.</param>
        Private Shared Sub PrintUserCalendars(ByVal service As CalendarService)
            Dim query As New FeedQuery()
            query.Uri = New Uri("http://www.google.com/calendar/feeds/default")

            ' Tell the service to query:
            Dim calFeed As AtomFeed = service.Query(query)

            Console.WriteLine("Your calendars:")
            Console.WriteLine()
            For i As Integer = 0 To calFeed.Entries.Count - 1
                Console.WriteLine(calFeed.Entries(i).Title.Text)
            Next
            Console.WriteLine()
        End Sub

        ''' <summary>
        ''' Prints the titles of all events on the specified calendar.
        ''' </summary>
        ''' <param name="service">The authenticated CalendarService object.</param>
        Private Shared Sub PrintAllEvents(ByVal service As CalendarService)
            Dim myQuery As New EventQuery(feedUri)
            Dim myResultsFeed As EventFeed = TryCast(service.Query(myQuery), EventFeed)

            Console.WriteLine("All events on your calendar:")
            Console.WriteLine()
            For i As Integer = 0 To myResultsFeed.Entries.Count - 1
                Console.WriteLine(myResultsFeed.Entries(i).Title.Text)
            Next
            Console.WriteLine()
        End Sub

        ''' <summary>
        ''' Prints the titles of all events matching a full-text query.
        ''' </summary>
        ''' <param name="service">The authenticated CalendarService object.</param>
        ''' <param name="queryString">The text for which to query.</param>
        Private Shared Sub FullTextQuery(ByVal service As CalendarService, ByVal queryString As String)
            Dim myQuery As New EventQuery(feedUri)
            myQuery.Query = queryString

            Dim myResultsFeed As EventFeed = TryCast(service.Query(myQuery), EventFeed)

            Console.WriteLine("Events matching ""{0}"":", queryString)
            Console.WriteLine()
            For i As Integer = 0 To myResultsFeed.Entries.Count - 1
                Console.WriteLine(myResultsFeed.Entries(i).Title.Text)
            Next
            Console.WriteLine()
        End Sub

        ''' <summary>
        ''' Prints the titles of all events in a specified date/time range.
        ''' </summary>
        ''' <param name="service">The authenticated CalendarService object.</param>
        ''' <param name="startTime">Start time (inclusive) of events to print.</param>
        ''' <param name="endTime">End time (exclusive) of events to print.</param>
        Private Shared Sub DateRangeQuery(ByVal service As CalendarService, ByVal startTime As DateTime, ByVal endTime As DateTime)
            Dim myQuery As New EventQuery(feedUri)
            myQuery.StartTime = startTime
            myQuery.EndTime = endTime

            Dim myResultsFeed As EventFeed = TryCast(service.Query(myQuery), EventFeed)

            Console.WriteLine("Matching events from {0} to {1}:", startTime.ToShortDateString(), endTime.ToShortDateString())
            Console.WriteLine()
            For i As Integer = 0 To myResultsFeed.Entries.Count - 1
                Console.WriteLine(myResultsFeed.Entries(i).Title.Text)
            Next
            Console.WriteLine()
        End Sub

        ''' <summary>
        ''' Helper method to create either single-instance or recurring events.
        ''' For simplicity, some values that might normally be passed as parameters
        ''' (such as author name, email, etc.) are hard-coded.
        ''' </summary>
        ''' <param name="service">The authenticated CalendarService object.</param>
        ''' <param name="entryTitle">Title of the event to create.</param>
        ''' <param name="recurData">Recurrence value for the event, or null for
        ''' single-instance events.</param>
        ''' <returns>The newly-created EventEntry on the calendar.</returns>
        Private Shared Function CreateEvent(ByVal service As CalendarService, ByVal entryTitle As String, ByVal recurData As String) As EventEntry
            Dim entry As New EventEntry()

            ' Set the title and content of the entry.
            entry.Title.Text = entryTitle
            entry.Content.Content = "Meet for a quick lesson."

            ' Set a location for the event.
            Dim eventLocation As New Where()
            eventLocation.ValueString = "South Tennis Courts"
            entry.Locations.Add(eventLocation)

            ' If a recurrence was requested, add it. Otherwise, set the
            ' time (the current date and time) and duration (30 minutes)
            ' of the event.
            If recurData Is Nothing Then
                Dim eventTime As New [When]()
                eventTime.StartTime = DateTime.Now
                eventTime.EndTime = eventTime.StartTime.AddMinutes(30)
                entry.Times.Add(eventTime)
            Else
                Dim recurrence As New Recurrence()
                recurrence.Value = recurData
                entry.Recurrence = recurrence
            End If

            ' Send the request and receive the response:
            Dim postUri As New Uri(feedUri)
            Dim insertedEntry As AtomEntry = service.Insert(postUri, entry)

            Return DirectCast(insertedEntry, EventEntry)
        End Function

        ''' <summary>
        ''' Creates a single-instance event on a calendar.
        ''' </summary>
        ''' <param name="service">The authenticated CalendarService object.</param>
        ''' <param name="entryTitle">Title of the event to create.</param>
        ''' <returns>The newly-created EventEntry on the calendar.</returns>
        Private Shared Function CreateSingleEvent(ByVal service As CalendarService, ByVal entryTitle As String) As EventEntry
            Return CreateEvent(service, entryTitle, Nothing)
        End Function

        ''' <summary>
        ''' Creates a recurring event on a calendar. In this example, the event
        ''' occurs every Tuesday from May 1, 2007 through September 4, 2007. Note
        ''' that we are using iCal (RFC 2445) syntax; see http://www.ietf.org/rfc/rfc2445.txt
        ''' for more information.
        ''' </summary>
        ''' <param name="service">The authenticated CalendarService object.</param>
        ''' <param name="entryTitle">Title of the event to create.</param>
        ''' <returns>The newly-created EventEntry on the calendar.</returns>
        Private Shared Function CreateRecurringEvent(ByVal service As CalendarService, ByVal entryTitle As String) As EventEntry
            Dim recurData As String = "DTSTART;VALUE=DATE:20070501" & Chr(13) & "" & Chr(10) & "" + "DTEND;VALUE=DATE:20070502" & Chr(13) & "" & Chr(10) & "" + "RRULE:FREQ=WEEKLY;BYDAY=Tu;UNTIL=20070904" & Chr(13) & "" & Chr(10) & ""

            Return CreateEvent(service, entryTitle, recurData)
        End Function

        ''' <summary>
        ''' Updates the title of an existing calendar event.
        ''' </summary>
        ''' <param name="entry">The event to update.</param>
        ''' <param name="newTitle">The new title for this event.</param>
        ''' <returns>The updated EventEntry object.</returns>
        Private Shared Function UpdateTitle(ByVal entry As EventEntry, ByVal newTitle As String) As EventEntry
            entry.Title.Text = newTitle
            Return DirectCast(entry.Update(), EventEntry)
        End Function

        ''' <summary>
        ''' Adds a reminder to a calendar event.
        ''' </summary>
        ''' <param name="entry">The event to update.</param>
        ''' <param name="numMinutes">Reminder time, in minutes.</param>
        ''' <returns>The updated EventEntry object.</returns>
        Private Shared Function AddReminder(ByVal entry As EventEntry, ByVal numMinutes As Integer) As EventEntry
            Dim reminder As New Reminder()
            reminder.Minutes = numMinutes
            entry.Reminder = reminder

            Return DirectCast(entry.Update(), EventEntry)
        End Function

        ''' <summary>
        ''' Adds an extended property to a calendar event.
        ''' </summary>
        ''' <param name="entry">The event to update.</param>
        ''' <returns>The updated EventEntry object.</returns>
        Private Shared Function AddExtendedProperty(ByVal entry As EventEntry) As EventEntry
            Dim [property] As New ExtendedProperty()
            [property].Name = "http://www.example.com/schemas/2005mycal.id"
            [property].Value = "1234"

            entry.ExtensionElements.Add([property])

            Return DirectCast(entry.Update(), EventEntry)
        End Function

        ''' <summary>
        ''' Retrieves and prints the access control lists of all
        ''' of the authenticated user's calendars.
        ''' </summary>
        ''' <param name="service">The authenticated CalendarService object.</param>
        Private Shared Sub RetrieveAcls(ByVal service As CalendarService)
            Dim query As New FeedQuery()
            query.Uri = New Uri("http://www.google.com/calendar/feeds/default")
            Dim calFeed As AtomFeed = service.Query(query)

            Console.WriteLine()
            Console.WriteLine("Sharing permissions for your calendars:")

            ' Retrieve the meta-feed of all calendars.
            For Each calendarEntry As AtomEntry In calFeed.Entries
                Console.WriteLine("Calendar: {0}", calendarEntry.Title.Text)
                Dim link As AtomLink = calendarEntry.Links.FindService(AclNameTable.LINK_REL_ACCESS_CONTROL_LIST, Nothing)

                ' For each calendar, retrieve its ACL feed.
                If link IsNot Nothing Then
                    Dim feed As AclFeed = service.Query(New AclQuery(link.HRef.ToString()))
                    For Each aclEntry As AclEntry In feed.Entries
                        Console.WriteLine("" & Chr(9) & "Scope: Type={0} ({1})", aclEntry.Scope.Type, aclEntry.Scope.Value)
                        Console.WriteLine("" & Chr(9) & "Role: {0}", aclEntry.Role.Value)
                    Next
                End If
            Next
        End Sub

        ''' <summary>
        ''' Shares a calendar with the specified user. Note that this method
        ''' will not run by default.
        ''' </summary>
        ''' <param name="service">The authenticated CalendarService object.</param>
        ''' <param name="aclFeedUri">the ACL feed URI of the calendar being shared.</param>
        ''' <param name="userEmail">The email address of the user with whom to share.</param>
        ''' <param name="role">The role of the user with whom to share.</param>
        ''' <returns>The AclEntry returned by the server.</returns>
        Private Shared Function AddAccessControl(ByVal service As CalendarService, ByVal aclFeedUri As String, ByVal userEmail As String, ByVal role As AclRole) As AclEntry
            Dim entry As New AclEntry()

            entry.Scope = New AclScope()
            entry.Scope.Type = AclScope.SCOPE_USER
            entry.Scope.Value = userEmail

            entry.Role = role

            Dim aclUri As New Uri("http://www.google.com/calendar/feeds/gdata.ops.test@gmail.com/acl/full")

            Dim insertedEntry As AclEntry = TryCast(service.Insert(aclUri, entry), AclEntry)
            Console.WriteLine("Added user {0}", insertedEntry.Scope.Value)

            Return insertedEntry
        End Function

        ''' <summary>
        ''' Updates a user to have new access permissions over a calendar.
        ''' Note that this method will not run by default.
        ''' </summary>
        ''' <param name="entry">An existing AclEntry representing sharing permissions.</param>
        ''' <param name="newRole">The new role (access permissions) for the user.</param>
        ''' <returns>The updated AclEntry.</returns>
        Private Shared Function UpdateEntry(ByVal entry As AclEntry, ByVal newRole As AclRole) As AclEntry
            entry.Role = newRole
            Dim updatedEntry As AclEntry = TryCast(entry.Update(), AclEntry)

            Console.WriteLine("Updated {0} to have role {1}", updatedEntry.Scope.Value, entry.Role.Value)
            Return updatedEntry
        End Function

        ''' <summary>
        ''' Deletes a user from a calendar's access control list, preventing
        ''' that user from accessing the calendar. Note that this method will
        ''' not run by default.
        ''' </summary>
        ''' <param name="entry">An existing AclEntry representing sharing permissions.</param>
        Private Shared Sub DeleteEntry(ByVal entry As AclEntry)
            entry.Delete()
        End Sub

        ''' <summary>
        ''' Runs the methods above to demonstrate usage of the .NET
        ''' client library. The methods that add, update, or remove
        ''' users on access control lists will not run by default.
        ''' </summary>
        Private Shared Sub RunSample()
            Dim service As New CalendarService("exampleCo-exampleApp-1")
            service.setUserCredentials(userName, userPassword)

            ' Demonstrate retrieving a list of the user's calendars.
            PrintUserCalendars(service)

            ' Demonstrate various feed queries.
            PrintAllEvents(service)
            FullTextQuery(service, "Tennis")
            DateRangeQuery(service, New DateTime(2007, 1, 5), New DateTime(2007, 1, 7))

            ' Demonstrate creating a single-occurrence event.
            Dim singleEvent As EventEntry = CreateSingleEvent(service, "Tennis with Mike")
            Console.WriteLine("Successfully created event {0}", singleEvent.Title.Text)

            ' Demonstrate creating a recurring event.
            Dim recurringEvent As AtomEntry = CreateRecurringEvent(service, "Tennis with Dan")
            Console.WriteLine("Successfully created recurring event {0}", recurringEvent.Title.Text)

            ' Demonstrate updating the event's text.
            singleEvent = UpdateTitle(singleEvent, "Important meeting")
            Console.WriteLine("Event's new title is {0}", singleEvent.Title.Text)

            ' Demonstrate adding a reminder. Note that this will only work on a primary
            ' calendar.
            singleEvent = AddReminder(singleEvent, 15)
            Console.WriteLine("Set a {0}-minute reminder for the event.", singleEvent.Reminder.Minutes)

            ' Demonstrate adding an extended property.
            AddExtendedProperty(singleEvent)

            ' Demonstrate deleting the item.
            singleEvent.Delete()

            ' Demonstrate retrieving access control lists for all calendars.
            RetrieveAcls(service)
        End Sub

        Private Shared Sub Main(ByVal args As String())
            If args.Length <> 3 Then
                Console.WriteLine("Usage: gcal_demo <username> <password> <feedUri>")
            Else
                userName = args(0)
                userPassword = args(1)
                feedUri = args(2)

                RunSample()
            End If
    End Sub
    Private Shared Sub Setup()
        ' Create a CalenderService and authenticate
        Dim myService As New CalendarService("exampleCo-exampleApp-1")
        myService.setUserCredentials("lsmiles247@gmail.com", "thanks2644")

        Dim query As New CalendarQuery()
        query.Uri = New Uri("http://www.google.com/calendar/feeds/default/allcalendars/full")
        Dim resultFeed As CalendarFeed = myService.Query(query)
        Console.WriteLine("Your calendars:" & Chr(10) & "")
        For Each entry As CalendarEntry In resultFeed.Entries
            Console.WriteLine(entry.Title.Text + "" & Chr(10) & "")
        Next

    End Sub
    End Class
